package com.xxwy.lintcodes;

import com.xxwy.StringXXwy;

/**
 * @author xxwy
 */
@StringXXwy
public class Num594 {
    public int strStr2(String source, String target) {
        // write your code here
        if (source == null || target == null) {
            return -1;
        }
        int m = target.length();
        int n = source.length();
        if (m == 0) {
            return 0;
        }
        if(m>n){
            return -1;
        }
        int targetHash = target.hashCode();

        for (int i = 0; i < n; i++) {
            if ((i + m - 1) >= n) {
                break;
            }
            String substring = source.substring(i, i + m);
            int subHash = substring.hashCode();
            if(targetHash == subHash && target.equals(substring)){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Num594 num594 = new Num594();
        int i = num594.strStr2("abcdef", "ef");
        System.out.println(i);
    }
}
